package com.daykw.wenshu.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.CourtAdhocUrl;
import com.daykw.wenshu.utils.Const;
import com.daykw.wenshu.utils.SpringContextHolder;
import com.daykw.wenshu.utils.TQueue;

//@DisallowConcurrentExecution
//@Service
public class ArticleAdhocJob extends Thread{

	private static Logger logger = Logger.getLogger(ArticleAdhocJob.class);


	public void run() {

		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
			Const c = SpringContextHolder.getBean(Const.class);
			int size = c.getArticle_num() * 2;
			java.util.Random random = new java.util.Random();
			int r = random.nextInt(100);

			int all = dao.count(CourtAdhocUrl.class, Cnd.where("handled", "=", "P"));
			List<CourtAdhocUrl> ds = new ArrayList<CourtAdhocUrl>();
			if (all < 10000) {
				ds = dao.query(CourtAdhocUrl.class, Cnd.wrap("handled = 'P'  LIMIT " + size));
			} else {
				ds = dao.query(CourtAdhocUrl.class,
						Cnd.wrap("handled = 'P' and mod(snatch_id," + 100 + ")=" + r + " LIMIT " + size));
			}
			int i = 0;
			for (CourtAdhocUrl task : ds) {
				TQueue.getCqueue().offer(task);
				i++;
			}
			logger.info("增加了" + i + "文章个任务");
		} catch (Exception e) {
			logger.error("", e);
		}

	}

}
